// components/home/index.js
Component({
  data: {
    longitude: 0,
    latitude: 0,
    markers: []
  },

 lifetimes: {
  load() {
    this.ctx = wx.createMapContext("map")
  },

  ready() {
    wx.getLocation({
      type: 'wgs84',
      success: (res) => {
        let { longitude, latitude } = res
        this.setData({
          longitude,
          latitude
        })
      }
    })
    
    wx.request({
      url: 'http://localhost:9000/data',
      success: (res) => {
        let markers = res.data.map((point) => {
          let { id, latitude, longitude, type} = point
          return {
            id,
            latitude,
            longitude,
            iconPath: `/resources/${type}.png`,
            height: '80rpx',
            width: '80rpx'
          }
        })
        this.setData({
          markers
        })
      }
    })
  },
 },

 methods: {
  handleCenterTap() {
    this.ctx.moveToLocation()
  },

  handleMarkerTap(e) {
    const id = e.detail.markerId
    wx.navigateTo({
      url: '/pages/detail/index?id=' + id,
    })
  },

  handleSearchTap(e) {
    wx.navigateTo({
      url: '/views/search/search',
    })
  }
 }
})
